package com.cxria.problem.web.controller

import com.cxria.problem.dto.PasswordDto
import com.cxria.problem.dto.Status
import com.cxria.problem.entity.User
import com.cxria.problem.service.UserService
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.util.MimeTypeUtils
import org.springframework.web.bind.annotation.*

@Controller
@RequestMapping('/user')
class UserController {

	final Logger logger = LoggerFactory.getLogger(this.getClass())

	@Autowired
	UserService userService

	@GetMapping(value = '/{userId}/show')
	@ResponseBody
	User show(@PathVariable(name = 'userId') Integer userId) {
		return userService.getById(userId)
	}

	@PostMapping(value = '/register', produces = [MimeTypeUtils.APPLICATION_JSON_VALUE])
	@ResponseBody
	Status save(@RequestBody User user) {
		userService.add(user)
		return new Status()
	}

	@PostMapping(value = '/{userId}/delete')
	@ResponseBody
	Status delete(@PathVariable(name = 'userId') long id){
		userService.delete(id)
		return new Status()
	}

	@RequestMapping(value = '/list', method = RequestMethod.GET)
	@ResponseBody
	List<User> list() {
		return userService.findAll()
	}

	@PostMapping(value = '/update/password')
	@ResponseBody
	Status modifyPassword(@RequestBody PasswordDto passwordDto){
		userService.updatePassword(passwordDto)
		return new Status()
	}

}
